﻿using Abp.Domain.Repositories;
using MyCompanyName.AbpZeroTemplate.Prm.Admin.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Prm.Admin
{
    public class SystemParamsAppService : AbpZeroTemplateAppServiceBase, ISystemParamsAppService
    {
        IRepository<SystemDic, int> SystemDicRepository;

        public SystemParamsAppService(IRepository<SystemDic, int> _SystemDicRepository)
        {
            SystemDicRepository = _SystemDicRepository;
        }

        public List<dynamic> GetCatalog()
        {

            List<dynamic> res = new List<dynamic>();

            foreach (string name in Enum.GetNames(typeof(DictKey)))
            {
                res.Add(new { id = Guid.NewGuid(), text = name, leaf = true });
            }
            return res;
        }


        public List<SystemDicOutPut> GetParams(string catalog)
        {

            DictKey _catelog = (DictKey)Enum.Parse(typeof(DictKey), catalog, true);
            var res = SystemDicRepository.GetAll().Where(x => x.DictKey == _catelog).OrderBy(x => x.DisplayName).Select(x => new SystemDicOutPut
            {
                Id = x.Id,
                ParameterName = x.DisplayName,
                ParameterValue = x.CodeValue,
                Sortfactor = x.Sortfactor
            }).ToList();
            return res;
        }

        public void SaveSubmit(SystemDicInPut input)
        {
            DictKey catelog = (DictKey)Enum.Parse(typeof(DictKey), input.Catalog, true);
            List<string> names = input.DisplayName.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList();

            foreach (var item in names)
            {
                if (string.IsNullOrEmpty(item))
                {
                    continue;
                }
                if (!SystemDicRepository.GetAll().Any(x => x.DictKey == catelog && x.DisplayName == item))
                {
                    SystemDicRepository.Insert(new SystemDic()
                    {
                        DisplayName = item,
                        DictKey = catelog,
                        CodeValue = input.CodeValue,
                        IsDeleted = false,
                        ItemKey = catelog.ToString()
                    });
                }
            }


        }



        public void ParamsUpdate(List<SystemDic> list)
        {
            foreach (var item in list)
            {
                var d = SystemDicRepository.Get(item.Id);
                d.DisplayName = item.DisplayName;
                d.CodeValue = item.CodeValue;
                SystemDicRepository.Update(d);
            }
        }


        public void Init()
        {
            #region initdata
            string 治疗领域 = @"
系统用抗感染药
肌肉-骨骼系统
呼吸系统
其他
抗肿瘤药和免疫机能调节药
非性激素和胰岛素类的激素类系统用药
感觉器官
皮肤病用药
血液和造血器官
肌肉系统
神经系统
抗寄生虫药、杀虫药和驱虫药
生殖泌尿系统和性激素
消化道及代谢
心血管系统
兽药
饲料添加剂
";
            string 项目类型 = @"
API
CDMO
CRO
";
            string 临床阶段 = @"
P0
P1
P2
P3
NDA
P4
Discountinued
";
            string 项目北京化合物用途 = @"
注册起始物料  
高级中间体     
API
";
            #endregion

            SaveSubmit(new SystemDicInPut()
            {
                Catalog = DictKey.治疗领域.ToString(),
                CodeValue = "",
                DisplayName = 治疗领域
            });
            SaveSubmit(new SystemDicInPut()
            {
                Catalog = DictKey.临床阶段.ToString(),
                CodeValue = "",
                DisplayName = 临床阶段
            });
            SaveSubmit(new SystemDicInPut()
            {
                Catalog = DictKey.项目背景化合物用途.ToString(),
                CodeValue = "",
                DisplayName = 项目北京化合物用途
            });
            SaveSubmit(new SystemDicInPut()
            {
                Catalog = DictKey.项目类型.ToString(),
                CodeValue = "",
                DisplayName = 项目类型
            });


        }

    }
}
